package com.rigsofrods.truckeditor.app;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.JOptionPane;

import com.rigsofrods.truckeditor.gui.MainFrame;

/*FIXME: Some recent files are not remembered */
public class TruckEditor {
	private File currentFile;
	private Deque<String> recentFiles = new LinkedList<String>();
	private static final int RECENT_FILES_COUNT = 8;
	private boolean fileChanged = false;
	private MainFrame mainFrame;
	
	private static Preferences prefs;
	private static final String RECENT_FILE = "recentFile";
	
	public TruckEditor() {
		prefs = Preferences.userNodeForPackage(getClass());
		for(int i = 0; i < RECENT_FILES_COUNT; i++){
			String path = prefs.get(RECENT_FILE + Integer.toString(i), null);
			if(path != null){
				recentFiles.add(path);
			}else{
				break;
			}
		}
		this.mainFrame = new MainFrame(this);
	}

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		List<String> a = new ArrayList<String>();
		for (int i = 0; i < args.length; i++) {
			a.add(args[i]);
		}
		File f = null;
		if(a.size() > 0){
			f = new File(a.get(0));
			if(f.isFile()){
				if (!f.canRead()) {
					JOptionPane.showMessageDialog(null, "Cannot open file " + f);
					f = null;
				}
			}
			if(a.contains("-clear")){
				System.out.println("Deleteng all preferences...");
				try {
					Preferences pu = Preferences.userNodeForPackage(TruckEditor.class).parent();
					pu.removeNode();					
					pu.flush();
					System.out.println("Preferences deleted");
					System.exit(0);
				} catch (BackingStoreException e) {
					e.printStackTrace();
				}
			}
		}
		TruckEditor te = new TruckEditor();
		te.mainFrame.setVisible(true);
		te.mainFrame.openFile(f);
	}

	public File getCurrentFile() {
		return currentFile;
	}

	public void setCurrentFile(File file) {
		if(!file.equals(currentFile) && currentFile != null){
			try {
				String fp = currentFile.getCanonicalPath();
				recentFiles.remove(fp);
				recentFiles.push(fp);
				while (recentFiles.size() > RECENT_FILES_COUNT) {
					recentFiles.pollLast();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			Deque<String> tmpRF = new LinkedList<String>(recentFiles);
			for(int i = 0; i < tmpRF.size(); i++){
				prefs.put(RECENT_FILE + Integer.toString(i), tmpRF.poll());
				System.out.println(RECENT_FILE + Integer.toString(i) + ": " + tmpRF.poll());
			}
			try {
				prefs.flush();
			} catch (BackingStoreException e) {
			}
		}
		currentFile = file;
		fileChanged = false;
		mainFrame.setTitle(currentFile.getName());
	}
	
	public Collection<String> getRecentFiles(){
		return Collections.unmodifiableCollection(recentFiles);
	}
	
	public void removeRecentFile(String path){
		try {
			recentFiles.remove(new File(path).getCanonicalPath());
		} catch (IOException e) {
		}
	}

	public boolean isFileChanged() {
		return fileChanged;
	}

	public void setFileChanged(boolean fileChanged) {
		this.fileChanged = fileChanged;
	}

}
